UNPKG

nuxt-users

Version:

A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools

32 lines (31 loc) 994 B
import { createError, defineEventHandler, readBody } from "h3"; import { useRuntimeConfig } from "#imports"; import { updateUser } from "../../utils/user.js"; export default defineEventHandler(async (event) => { const { nuxtUsers } = useRuntimeConfig(); const options = nuxtUsers; const userId = Number(event.context.params?.id); if (!userId) { throw createError({ statusCode: 400, statusMessage: "Invalid user ID" }); } const body = await readBody(event); const currentUser = event.context.user; if (currentUser && currentUser.id === userId && typeof body.active !== "undefined") { throw createError({ statusCode: 403, statusMessage: "You cannot change your own active status." }); } try { const updatedUser = await updateUser(userId, body, options); return { user: updatedUser }; } catch (error) { throw createError({ statusCode: 500, statusMessage: `Error updating user: ${error.message}` }); } });